package smashedapples.core
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.getQualifiedClassName;
	
	public class BizRule extends NonVisualComponent
	{
		
	
		public function BizRule()
		{
			super();
		}
		
		
		public var name:String;
		
		public function execute():void
		{
			trace( "execute biz rule");
		}
		
		
		public var target:EventDispatcher;
		
		
		private var _listeners:Array = [];
		public function get listeners():Array
		{
			return _listeners;
		}
		public function set listeners( value:Array ):void
		{
			_listeners = value;
			//this.addPropertyChanged( "listeners" );
			onCommitListeners();
		}
		
		public function onCommitListeners():void
		{
			for each( var type:String in this.listeners )
			{
				this.addListener( type );
			}
		}
		
		public function addListener( type:String ):void
		{
			
			if( target != null )
			{
				target.addEventListener( type, handleEvent );
			}
				
		}
		
		private function handleEvent( event:Event ):void
		{
			var handlerName : String = getEventHandlerName( event );

			trace("BizRule::" + handlerName +  "(" + flash.utils.getQualifiedClassName(event) + ")");
			if( this.hasOwnProperty(handlerName))
			{
				var method : Function = this[handlerName] as Function;
				method.apply( this, [event] );
			}
			
		}
		
		protected function getEventHandlerName( event:Event ):String
		{
			return "on" + event.type.substr(0,1).toUpperCase() + event.type.slice(1);
		}
		
		
		
		
		public function addHandler( method:Function ):void
		{
			
		}
		
		
		
		
		
	}
}